// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-04-16 14:38
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.ACWing.DataTrains.Moo_encryption;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Moo_encryption {
    public static void main(String[] args) {
        Scanner myScanner = new Scanner(System.in);
        int n = myScanner.nextInt();
        int m = myScanner.nextInt();
        char[][] map = new char[n][m];
        /*-----地图信息初始化-----*/
        for (int i = 0; i < n; i++) {
            String s = myScanner.next();
            for (int j = 0; j < m; j++) {
                map[i][j] = s.charAt(j);
            }
        }
        /*-----获取通过不同加密可以获得的最大MOO数量-----*/
        // 通过遍历找出类似于ABB形式的串，并且通过Hash统计数量
        // 有八个方向
        int[] x = {1, -1, 0, 0, -1, 1, 1, -1};
        int[] y = {0, 0, -1, 1, -1, 1, -1, 1};
        Map<String, Integer> ans = new HashMap<>();
        int max = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                for (int k = 0; k < 8; ++k) {
                    char a = map[i][j];
                    int newX = i + x[k], newXX = newX + x[k];
                    int newY = j + y[k], newYY = newY + y[k];
                    if (newX < 0 || newX >= n || newY < 0 || newY >= m || newXX < 0 || newXX >= n || newYY < 0 || newYY >= m) {
                        continue;
                    }
                    char b = map[newX][newY], c = map[newXX][newYY];
                    if (a != b && b == c && a != 'M' && b != 'O') {
                        String key = String.valueOf(a) + b + c;
                        ans.put(key, ans.getOrDefault(key, 0) + 1);
                        max = Math.max(max, ans.get(key));
                    }

                }
            }
        }
        System.out.print(max);
    }
}
